Ext.ns("Ext.baiyue");

var webPath = "/bookshop-admin";
Ext.baiyue.AdminUpdateOrSave = function(config){
	var fieldHeight = 22;
	var fieldWidth  = 300;
	var labelWidth  = 70;
	var panelHeight = 250;
	var idField = new Ext.form.TextField({
		name       : "adminId",
		fieldLabel : "管理员ID",
		value      : config.adminId,
		height     : fieldHeight,
		width      : fieldWidth,
		labelWidth : labelWidth,
		readOnly   : true,
	});
	var nameField = new Ext.form.TextField({
		name       : "name",
		fieldLabel : "管理员名称",
		value      : config.name,
		height     : fieldHeight,
		width      : fieldWidth,
		inputType  : 'text',
		labelWidth : labelWidth,
		disabled   : false,
	});
	var oldPwField = new Ext.form.TextField({
		name       : "oldPassword",
		fieldLabel : "密码",
		value      : "",
		height     : fieldHeight,
		width      : fieldWidth,
		disabled   : false,
		inputType  : 'password',
		labelWidth : labelWidth,
		blankText  	: "请输入密码(root输入自身密码)",
		emptyText   : "请输入密码(root输入自身密码)",
	});
	var pwField = new Ext.form.TextField({
		id         : "newPassword",
		name       : "newPassword",
		fieldLabel : "新密码",
		height     : fieldHeight,
		width      : fieldWidth,
		disabled   : false,
		inputType  : 'password',
		labelWidth : labelWidth,
		blankText  	: "请输入密码(不修改密码无需)",
		emptyText   : "请输入密码(不修改密码无需)",
	});
	var pwValidField = new Ext.form.TextField({
		fieldLabel : "重输入",
		height     : fieldHeight,
		width      : fieldWidth,
		disabled   : false,
		inputType  : 'password',
		labelWidth : labelWidth,
		blankText  : "请再次输入密码",
		emptyText  : "请再次输入密码",
		vtype	   : 'password',  
        vtypeText  : "两次密码不一致",  
        confirmTo  : "newPassword", 
	});
	var emailField = new Ext.form.TextField({
		name       : "email",
		fieldLabel : "邮件",
		value      : config.email,
		height     : fieldHeight,
		width      : fieldWidth,
		disabled   : false,
		inputType  : 'text',
		labelWidth : labelWidth,
		vtype      : "email",
	});
	var roleField = new Ext.form.TextField({
		name       : 'role',
		fieldLabel : "角色",
		value      : config.role,
		height     : fieldHeight,
		width      : fieldWidth,
		disabled   : false,
		inputType  : 'text',
		labelWidth : labelWidth,
	});
	//校验函数
	Ext.apply(Ext.form.VTypes,
		{
		password:function(val,field){   
			if(field.confirmTo){   
				var tmp=Ext.getCmp(field.confirmTo).getValue();                     
				if(val == tmp){   
					return true;   
				} else {   
					return false;   
				}   
			}
		},
		email:function(val,field){
			   return /..*@.*\...*/.test(val);
		}
	});
	//取消按钮
	 var cancelButton = new Ext.button.Button({
	        text   : "取消",
	        margin : '5 0 5 5',
	        handler: function() {
	            controlWindow.close();
	        }
	 });
	 //确定按钮
	 var sureButtion = new Ext.button.Button({
		 	text   : "确定",
	        margin : '5 0 5 5',
	        handler: function() {
	        	var targetForm = formPanel.getForm();
	            if (targetForm.isValid()) {
	                targetForm.submit({
	                    url: config.url,
	                    waitMsg: "正在更新...",
	                    success: function(resp,opts) {
	                    	Ext.Msg.alert("温馨提示",opts.result.message); 
	                    	adminMain.getStore().reload();
	                        controlWindow.close();
	                    },
	                    failure: function(resp,opts) {
	                    	Ext.Msg.alert("温馨提示",opts.result.message);
	                		//controlWindow.close();
	                    }
	               });   
	            }
	        }
	 });

	var formPanel = new Ext.FormPanel({
		margin      : "2 2 2 2",
		header		: false,
        width       : fieldWidth+5,
        height      : panelHeight,
        items       : [
                idField,
                nameField,
                oldPwField,
                pwField,
                pwValidField,
                emailField,
                roleField,
            {
                xtype	: 'toolbar',
                width	: fieldWidth,
                height  : fieldHeight*2,
                header	: false,
                border	: false,
                closable: false,
                draggable: false,
                frame	: true,
                items	: [ { xtype: 'tbfill' }, sureButtion, { xtype: 'tbfill' }, cancelButton, { xtype: 'tbfill' } ]
            }]
	});
	if(config.insert){
		var d = formPanel.items.items;
		//隐藏idField
		d[0].hidden = true;
		//隐藏oldPwField
		d[2].hidden = true;
		panelHeight = panelHeight - 50;
	}
    var controlWindow = Ext.create('Ext.window.Window', {
        title	: config.title,
        layout	: 'border',
        id		: 'modifyAdmin',
        constrainHeader:true,
        width	: fieldWidth+10,
        height  : panelHeight+20,
        resizable: false,
        items: [ formPanel ]
    });
    
    return controlWindow;
};